﻿using System;
using System.Collections.Generic;
using System.Linq;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using System.Web.UI.WebControls;

namespace WebApplication
{
    public partial class MediaRoomImg : BasePage
    {
        protected string CustUID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string cuid = Request.QueryString[Constant.UrlParamName.CustomerUID];
                if (!string.IsNullOrEmpty(cuid))
                {
                    CustUID = cuid;
                    BindDataSource();
                    hyVideo.NavigateUrl = "MediaRoomVideo.aspx?cuid="+CustUID;
                }
            }
        }

        private void BindDataSource()
        {
            tblMediaRoomBLL bll = new tblMediaRoomBLL();
            List<tblMediaRoom> listAll = new List<tblMediaRoom>();
            tblMediaRoom entPara = new tblMediaRoom();
            entPara.CustomerUID = int.Parse(CustUID);
            entPara.MediaType = Constant.MediaType.image;
            entPara.IsPublish = -1;
            listAll = bll.GetListByENT(entPara);

            rp_CustomerLogo.DataSource = listAll.Where(a => a.Category == Constant.MediaRoomCategory.customer_logo);
            rp_CustomerLogo.DataBind();

            rp_ContactPersonPhoto.DataSource = listAll.Where(a => a.Category == Constant.MediaRoomCategory.contactperson_photo);
            rp_ContactPersonPhoto.DataBind();

            rp_BranchImage.DataSource = listAll.Where(a => a.Category == Constant.MediaRoomCategory.branch_image);
            rp_BranchImage.DataBind();

            rp_BrandLogo.DataSource = listAll.Where(a => a.Category == Constant.MediaRoomCategory.brand_logo);
            rp_BrandLogo.DataBind();

            rp_ProductImage.DataSource = listAll.Where(a => a.Category == Constant.MediaRoomCategory.product_image);
            rp_ProductImage.DataBind();
        }

        #region 辅助方法
        protected bool displayDelete(object mediaroomUID)
        {
            tblMediaRoomRelatedBLL bllMrr = new tblMediaRoomRelatedBLL();
            tblMediaRoom_Related entMrr = new tblMediaRoom_Related();
            entMrr.MediaRoomUID = int.Parse(mediaroomUID.ToString());
            entMrr.Deleted = Constant.IsDelete.no;
            int count = bllMrr.GetCountByENT(entMrr);
            if (count > 0) {
                return false;
            }
            return true;
        }

        protected string CreateDeleteHtml(object mediaroomUID)
        {
            string ret = "<span class='closeBtnbig' style='color:#ccc;' title='该图片正被其他地方引用,暂时不能删除.'><b>Delete</b></span";
            if (displayDelete(mediaroomUID))
            {
                ret = string.Format(" <span class='closeBtnbig'><a href='javascript:delMediaRoom({0})'>Delete</a></span>", mediaroomUID);
            }
            return ret;
        }
        
        #endregion
    }
}